package com.attendance.chdingtask.service;

import com.attendance.chdingtask.dao.BaseJPA;
import com.attendance.chdingtask.dao.MdDetailsDao;
import com.attendance.chdingtask.model.*;
import com.attendance.chdingtask.utils.common.Page;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Locale;

@Service
public class MdDetailsService extends BaseService<MdDetails,Long>{

    @Autowired
    private JPAQueryFactory queryDslTool;
    @Autowired
    private MdDetailsDao dao;

    @Override
    protected BaseJPA<MdDetails> getDao() {
        return dao;
    }
    public void findUserByData(UserQryParm vo, Page<MdDetails> page, String corpId) {
        QMdDetails qMdDetails=QMdDetails.mdDetails;
        JPAQuery<MdDetails> jpaQuery=queryDslTool.select(qMdDetails).from(qMdDetails);
        if(!isEmpty(vo.getUserName())){
            jpaQuery.where(qMdDetails.userName.contains(vo.getUserName()));
        }
        if(!isEmpty(vo.getDeptListId())){
            jpaQuery.where(qMdDetails.deptName.contains(vo.getDeptListId()));
        }
        if(null != vo.getStartDate()){
            jpaQuery.where(qMdDetails.recognizeTime.after(vo.getStartDate()));
        }
        if(null != vo.getEndDate()) {
            jpaQuery.where(qMdDetails.recognizeTime.before(vo.getEndDate()));
        }
        if(null != vo.getAreaName()){
            jpaQuery.where(qMdDetails.areaName.eq(vo.getAreaName()));
        }
        jpaQuery.where(qMdDetails.dr.eq(0));
        jpaQuery.where(qMdDetails.corpId.eq(corpId));
        jpaQuery.orderBy(qMdDetails.createTime.desc());

        long count=jpaQuery.fetchCount();
        List<MdDetails> list=jpaQuery.offset((page.getPageCurrent()-1)*page.getPageSize())
                .limit(page.getPageSize())
                .fetch();
        page.setList(list);
        page.setTotal(count);


    }

    public List<MdDetails> findDataByType(UserQryParm vo, MdArea area,String flag, String corpId) {

        return dao.findAllByRecognizeTimeBeforeAndRecognizeTimeAfterAndAreaIdAndCorpIdAndFlag(vo.getEndDate(),vo.getStartDate(),area.getId(),corpId,flag);
    }


    public List<MdDetails> findDataByDeviceId(UserQryParm vo, Long deviceId, String corpId) {

        return dao.findAllByRecognizeTimeBeforeAndRecognizeTimeAfterAndDeviceIdAndCorpId(vo.getEndDate(),vo.getStartDate(),deviceId,corpId);
    }

    public List<MdDetails> findExpData(Date startDate, Date endDate) {
        return dao.findAllByRecognizeTimeBeforeAndRecognizeTimeAfter(startDate,endDate);
    }

    public List<MdDetails> findDataByDeviceIds(UserQryParm vo, Long id) {
        return dao.findAllByRecognizeTimeBeforeAndRecognizeTimeAfterAndDeviceId(vo.getStartDate(),vo.getStartDate(),id);
    }
}
